package com.xxxy.jsq.action.record;

import com.alibaba.fastjson2.JSONObject;
import com.xxxy.jsq.dao.RecordDao;
import com.xxxy.jsq.model.Page;
import com.xxxy.jsq.model.Record;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/getAllRecordsPageServlet")
public class getAllRecordsPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        Record model = new Record();
        RecordDao dao = new RecordDao();
        Page page = new Page();

        // 设置查询条件
        if(request.getParameter("recordName") != null && !request.getParameter("recordName").isEmpty()) {
            model.setRecordName(request.getParameter("recordName"));
        }
        if(request.getParameter("recordFlag") != null && !request.getParameter("recordFlag").isEmpty()) {
            model.setRecordFlag(request.getParameter("recordFlag"));
        }

        // 设置分页参数
        if(request.getParameter("page") != null && request.getParameter("limit") != null) {
            page.setCurrentPage(Integer.parseInt(request.getParameter("page")));
            page.setCount(Integer.parseInt(request.getParameter("limit")));
        }

        List<Record> list = dao.getAllDataByPage(model, page);
        int total = dao.getCount(model);

        JSONObject jo = new JSONObject();
        jo.put("code", 0);
        jo.put("msg", "success");
        jo.put("count", total);
        jo.put("data", list);

        PrintWriter out = response.getWriter();
        out.print(jo.toString());
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    } 
}